package org.jdns.jfreechess.draw;

import java.awt.Color;
import java.util.Set;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import java.util.prefs.Preferences;

import com.google.common.base.Strings;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Sets;

/*
 * Copyright (C) 2012 James Ring (sjr@jdns.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

public class BoardPreferences {
  private Set<BoardPreferenceListener> listeners = Sets.newLinkedHashSet();
  private final Preferences userPrefs;
  private PieceSet pieceSet;

  private static final Color DEFAULT_WHITE_SQUARE_COLOR = new Color(185, 240, 200);
  private static final Color DEFAULT_BLACK_SQUARE_COLOR = new Color(60, 180, 90);

  public BoardPreferences(Preferences userPrefs) {
    this.userPrefs = userPrefs;

    userPrefs.addPreferenceChangeListener(new PreferenceChangeListener() {
      @Override
      public void preferenceChange(PreferenceChangeEvent arg0) {
        firePreferenceChanged();
      }
    });
  }

  public void addBoardPreferenceListener(BoardPreferenceListener l) {
    listeners.add(l);
  }

  public void removeBoardPreferenceListener(BoardPreferenceListener l) {
    listeners.remove(l);
  }

  public Color getWhiteSquareColor() {
    return getColor("white_square_color", DEFAULT_WHITE_SQUARE_COLOR);
  }

  public Color getBlackSquareColor() {
    return getColor("black_square_color", DEFAULT_BLACK_SQUARE_COLOR);
  }

  public void setWhiteSquareColor(Color color) {
    setColor("white_square_color", color);
  }

  public void setBlackSquareColor(Color color) {
    setColor("black_square_color", color);
  }

  private Color getColor(String prefName, Color defaultColor) {
    String colorString = userPrefs.get(prefName, "");
    if (Strings.isNullOrEmpty(colorString) || colorString.charAt(0) != '#') {
      return defaultColor;
    }

    try {
      return new Color(Integer.parseInt(colorString, 16));
    } catch (NumberFormatException e) {
      return defaultColor;
    }
  }

  private void setColor(String prefName, Color theColor) {
    userPrefs.put(prefName, String.format("#%x", theColor.getRGB()));
    firePreferenceChanged();
  }

  private void firePreferenceChanged() {
    for (BoardPreferenceListener l : listeners) {
      l.preferenceChanged();
    }
  }

  public PieceSet getPieceSet() {
    if (pieceSet == null) {
      pieceSet = new PieceSet("Dyche");
    }

    return pieceSet;
  }
}
